//
//  CZKeyboardView.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/4.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
     func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(keyboardToolbar)
        addSubview(emotionView)
        addSubview(pageControl)
        
        keyboardToolbar.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
       emotionView.snp_makeConstraints { (make) in
        make.leading.trailing.top.equalTo(self)
        make.bottom.equalTo(keyboardToolbar.snp_top)
        }
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emotionView)
            make.centerX.equalTo(self)
        }
        
        //点击底部toolbar上的button
        keyboardToolbar.emoticonClosure = {[weak self] (type: CZKeyboardToolbarType) in
            
            let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self?.emotionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            self?.pageControl.numberOfPages = CZEmoticonTools.sharedEmoticonTools.allEmoticons[indexPath.section].count
            self?.pageControl.currentPage = 0
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var keyboardToolbar: CZKeyboardToolbar = CZKeyboardToolbar()
    private lazy var emotionView: CZEmotionCollectionView = {
        let view = CZEmotionCollectionView()
        view.bounces = false
        view.delegate = self
        view.pagingEnabled = true
        
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
    }()
    private lazy var pageControl: UIPageControl = {
       let PC = UIPageControl()
        PC.numberOfPages = CZEmoticonTools.sharedEmoticonTools.allEmoticons[0].count
        PC.currentPage = 0
        
        PC.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        PC.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
        return PC
    }()
}
extension CZKeyboardView: UICollectionViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let centerX = SCREENW / 2 + scrollView.contentOffset.x
        
        let cellArray = emotionView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if cellArray.count == 2 {
            
            let firstCell = cellArray.first!
            let lastCell = cellArray.last!
            //显示多的按钮的indexPath
            var indexPath: NSIndexPath
            
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = emotionView.indexPathForCell(firstCell)!
            } else {
                indexPath = emotionView.indexPathForCell(lastCell)!
            }
            
            keyboardToolbar.setCurrentButtons(indexPath.section)
            
            pageControl.numberOfPages = CZEmoticonTools.sharedEmoticonTools.allEmoticons[indexPath.section].count
            pageControl.currentPage = indexPath.item
        }
    }
}